/**
 * @author 黄波
 * 创建日期：2011Oct 12, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.bean.PageBean;
import com.platform.njcc.bean.ResUserMapBean;
import com.platform.os.util.Util;
import com.platform.pojo.DownloadRecord;
import com.platform.pojo.ResInfo;
import com.platform.pojo.ResReviews;
import com.platform.pojo.ResType;
import com.platform.pojo.UserInfo;
import com.platform.res.bean.ResInfoBean;
import com.platform.res.bo.ResBo;
import com.platform.user.bo.UserBo;

/**
 * 查询我上传或我下载的资源
 */
public class LoadCustomResListAction extends NJCCSuport {

	private String type;	//查询类型
	private List<ResInfoBean> items = new ArrayList<ResInfoBean>();	//资源列表
	private PageBean pageBean;	//分页实体
	private List<ResType> alltypes = new ArrayList<ResType>();	//所有资源类型列表
	private List<ResUserMapBean> beans = new ArrayList<ResUserMapBean> ();	//资源与用户映射关系

	@Override
	public String execute() throws Exception {
		//判断当前用户有没有登录
		HttpSession session = ServletActionContext.getRequest().getSession();
		UserInfo user = (UserInfo) session.getAttribute("user");
		if(user == null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.flush();
			out.write("<script>alert('请先登录！');history.back();</script>");
			out.close();
			return null;
		}
		
		ResBo resBo = ResBo.getInstance();
		UserBo userBo = UserBo.getInstance();
		//获得一级资源类型列表
		alltypes = resBo.getTypes();
		if(type != null && type.equals("myupload")) {
			//查询我上传的资源
			Object[] objs = resBo.getMyUploadRes(user.getId(), pageBean);
			pageBean = (PageBean) objs[0];
			List<ResInfo> resources = (List<ResInfo>) objs[1];
			for(ResInfo item : resources) {
				ResInfoBean bean = new ResInfoBean();
				bean.setInfo(item);
				if(alltypes != null) {
					ResType tmp = getTypeName(item.getTypeId(), alltypes);
					bean.setTypeName(tmp != null ? tmp.getName() : "无");
				}
				UserInfo user1 = userBo.getUser(item.getUploadUserId().toString());
				if(user1 != null) {
					bean.setUserName(user1.getRealName());
				} else {
					bean.setUserName(null);
				}
//				bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
				bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd"));
				bean.setDownloadCount(resBo.getDownloadCount(item.getId()).toString());
				items.add(bean);
			}
		} else if(type != null && type.equals("mydownload")) {
			//查询我下载的资源，给我已下载的资源一个标识，标识该资源是否已被我评论
			//已评论的资源不显示评论按钮，显示详情按钮
			//分页查询我的下载记录
			Object[] objs = resBo.getMyDownloadRes(user.getId(), pageBean);
			pageBean = (PageBean) objs[0];
			List<Object[]> objlist = (List<Object[]>) objs[1];
			
			for(Object[] obj : objlist) {
				ResUserMapBean bean = new ResUserMapBean();
				ResInfo resObj = (ResInfo) obj[0];
				DownloadRecord recordObj = (DownloadRecord) obj[1];
				ResReviews reviewObj = resBo.getResReview(user.getId().toString(), resObj.getId().toString());
				bean.setRes(resObj);
				bean.setUser(user);
				bean.setRecord(recordObj);
				bean.setReviews(reviewObj);
				if(alltypes != null) {
					ResType tmp = getTypeName(resObj.getTypeId(), alltypes);
					bean.setTypeName(tmp != null ? tmp.getName() : "无");
				}
				beans.add(bean);
			}
		}
		return SUCCESS;
	}
	/**
	 * @param typeId
	 * @return
	 */
	private ResType getTypeName(Integer typeId, List<ResType> types) {
		for(ResType item : types) {
			if(item.getId() == typeId) {
				return item;
			}
		}
		return null;
	}

	public List<ResUserMapBean> getBeans() {
		return beans;
	}
	public void setBeans(List<ResUserMapBean> beans) {
		this.beans = beans;
	}
	
	public List<ResType> getAlltypes() {
		return alltypes;
	}

	public void setAlltypes(List<ResType> alltypes) {
		this.alltypes = alltypes;
	}

	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public List<ResInfoBean> getItems() {
		return items;
	}

	public void setItems(List<ResInfoBean> items) {
		this.items = items;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
}
